<template>
  <div>
    <el-form
      ref="relationFormRef"
      :rules="rules"
      :model="relationForm.data"
      label-width="120"
    >
      <el-form-item label="感知模式">
        <el-input
          v-model="relationForm.data.sensingMode"
          disabled
          style="width: 320px"
          placeholder="请输入班组名称"
        />
      </el-form-item>
      <el-form-item label="设备编号">
        <el-input
          v-model="relationForm.data.deviceCode"
          disabled
          style="width: 320px"
          placeholder="请输入班组名称"
        />
      </el-form-item>
      <el-form-item label="归属行政区" prop="regionId">
        <el-cascader
          v-model="relationForm.data.regionId"
          :options="regionId.data"
          style="width: 320px"
          placeholder="请选择所属行政区"
          :props="{
            value: 'id',
            label: 'name',
            children: 'nodes',
          }"
          @change="handleCascaderChange"
          clearable
        />
      </el-form-item>
      <el-form-item label="归属路段" prop="sectionId">
        <el-select
          v-model="relationForm.data.sectionId"
          filterable
          style="width: 320px"
          placeholder="请选择归属路段"
          @change="handleCascaderChange2"
        >
          <el-option
            v-for="item in sectionId.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="泊位编号" prop="berthCodes">
        <el-select
          v-model="relationForm.data.berthCodes"
          filterable
          multiple
          style="width: 320px"
          placeholder="请选择泊位编号"
        >
          <el-option
            v-for="item in berthCodes.data"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(relationFormRef)"
              >重 置</el-button
            >
            <el-button class="confirm-btn" @click="submitForm(relationFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { getCanton, getRoadList, getBerthList } from "@/service/modules/publicModule";
import { getRelationBerth } from "../request.js";

const emits = defineEmits(["operationDone"]);

const props = defineProps({
  operationId: {
    type: Object,
    default: () => {},
  },
});

const regionId = reactive({
  data: [],
});

const sectionId = reactive({
  data: [],
});

const berthCodes = reactive({
  data: [],
});

const relationFormRef = ref("");
const relationForm = reactive({
  data: {
    sensingMode:
      props.operationId.sensingMode === "1"
        ? "地磁传感器"
        : props.operationId.sensingMode === "2"
        ? "视频桩"
        : props.operationId.sensingMode === "3"
        ? "高位视频(双目)"
        : "高位视频(球机)",
    deviceCode: props.operationId.deviceCode,
    regionId: props.operationId.regionId,
    sectionId: props.operationId.sectionId,
    berthCodes: props.operationId.berthCodes,
  },
});

const getData = async () => {
  await getCanton().then((res) => {
    regionId.data = res.data.result;
  });
  await getRoadList(relationForm.data.regionId).then((res) => {
    sectionId.data = res.data.result;
  });
  await getBerthList(relationForm.data).then((res) => {
    berthCodes.data = res.data.result;
  });
};

getData();

const handleCascaderChange = async (value) => {
  if (value !== null && value.length > 0) {
    relationForm.data.regionId = value[value.length - 1];
  } else {
    relationForm.data.regionId = "";
  }
  await getRoadList(relationForm.data.regionId).then((res) => {
    sectionId.data = res.data.result;
  });
};

const handleCascaderChange2 = async (value) => {
  await getBerthList(relationForm.data).then((res) => {
    berthCodes.data = res.data.result;
  });
};

const rules = reactive({
  regionId: [{ required: true, message: "请选择归属行政区", trigger: "change" }],
  sectionId: [{ required: true, message: "请选择归属路段", trigger: "change" }],
  berthCodes: [{ required: true, message: "请选择泊位编号", trigger: "blur" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getRelationBerth({
        deviceCode: relationForm.data.deviceCode,
        berthCodes: relationForm.data.berthCodes,
      }).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  relationForm.data.regionId = "";
  relationForm.data.sectionId = "";
  relationForm.data.berthCodes = "";
  formEl.resetFields();
};
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>
